<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="排查类型" prop="checkTypeId">
        <common-check-type-select  v-model="queryParams.checkTypeId" :parentId="0" placeholder="请选择排查类型" clearable @change="handleQuery"></common-check-type-select>
      </el-form-item>
      <el-form-item label="养护作业项目" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入养护作业项目" clearable @change="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cases:quantitativeType:add']">新建</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" :index="(index)=>tableIndex(queryParams.pageSize,queryParams.pageNum,index)" label="序号" align="center" />
      <el-table-column label="养护作业项目" align="center" prop="projectName" />
      <el-table-column label="排查类型" align="center" prop="checkTypeName" />
      <el-table-column label="每日额定完成工程量" align="center" prop="ratedNum">
        <template slot-scope="scope">
          <span>{{ scope.row.ratedNum + scope.row.ratedUnitName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人工单位成本" align="center" prop="laborCost" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.laborCost  + scope.row.costUnitName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cases:quantitativeType:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['cases:quantitativeType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="800px" append-to-body :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="排查类型" prop="checkTypeId">
              <common-check-type-select  v-model="form.checkTypeId" :parentId="0" placeholder="请选择排查类型" clearable></common-check-type-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="养护作业项目" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入养护作业项目" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="每日额定完成工作量" prop="ratedNum">
          <el-input type="number" style="width:200px" clearable v-model="form.ratedNum" placeholder="数量" />
          <dict-select class="ml10" style="width:100px" dict="jl_case_unit" v-model="form.ratedUnit"></dict-select>
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="人工单位成本" prop="laborCost">
              <el-input type="number" style="width:200px" clearable v-model="form.laborCost" placeholder="金额" />
              <dict-select class="ml10" style="width:100px" dict="jl_case_unit" v-model="form.costUnit"></dict-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isDisabled">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuantitativeType,
  getQuantitativeType,
  delQuantitativeType,
  addQuantitativeType,
  updateQuantitativeType,
} from "@/api/cases/quantitativeType";
import DictSelect from "@/components/DictSelect";
import CommonCheckTypeSelect from '../../../components/Select/CommonCheckTypeSelect.vue';

export default {
  name: "quantitativeType",
  components: { DictSelect,CommonCheckTypeSelect },
  data() {
    return {
      // 禁用表单
      isDisabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定额定量管理表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: void 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkTypeId: [
          { required: true, message: "排查类型不能为空", trigger: "change" },
        ],
        projectName: [
          { required: true, message: "养护作业项目不能为空", trigger: "blur" },
        ],
        ratedNum: [
          { required: true, message: "每日额定完成工作量不能为空", trigger: "blur" },
        ],
        ratedUnit: [
          { required: true, message: "每日额定完成工作量单位不能为空", trigger: "change" },
        ],
        laborCost: [
          { required: true, message: "人工单位成本不能为空", trigger: "blur" },
        ],
        costUnit: [
          { required: true, message: "人工单位成本单位不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
  },
  methods: {
    /** 查询定额定量管理列表 */
    getList() {
      this.loading = true;
      listQuantitativeType(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: void 0,
        checkTypeId: void 0,
        projectName: void 0,
        ratedNum: void 0,
        ratedUnit: void 0,
        laborCost: void 0,
        costUnit: void 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isDisabled = false;
      this.open = true;
      this.title = "养护定额定量标准-新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isDisabled = false;
      const id = row.id || this.ids;
      getQuantitativeType(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改定额定量";
      });
    },
    /** 查看 */
    handleDetail(row) {
      this.reset();
      this.isDisabled = true;
      const id = row.id || this.ids;
      getQuantitativeType(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看定额定量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQuantitativeType(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.handleClose();
              this.getList();
            });
          } else {
            addQuantitativeType(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.handleClose()
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除此定额定量的数据项？')
        .then(function () {
          return delQuantitativeType(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /**
     * 处理关闭
     */
    handleClose() {
      this.cancel();
    },
    /**
     * 隐藏
     */
    cancel() {
      this.open = false;
    },
  },
};
</script>
<style scoped></style>
